<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/8/26
 * Time: 13:24
 */

namespace app\service;


use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\SDK\ViapiUtils\ViapiUtils;

class OcrService
{
    function OcrIdCard($imgUrl)
    {
        if (!empty($imgUrl)) {
            AlibabaCloud::accessKeyClient('LTAI4G9Q8mU7xavjS1zL1y4q', 'FQxWmiftABjugBx2S3zFtiH6yrZUDW')
                ->regionId('cn-shanghai')
                ->asDefaultClient();
            try {
                $result = AlibabaCloud::rpc()
                    ->product('ocr')
                    // ->scheme('https') // https | http
                    ->version('2019-12-30')
                    ->action('RecognizeIdentityCard')
                    ->method('POST')
                    ->host('ocr.cn-shanghai.aliyuncs.com')
                    ->options([
                        'query' => [
                            'RegionId' => "cn-shanghai",
                            'ImageURL' => ViapiUtils::upload('LTAI4G9Q8mU7xavjS1zL1y4q', 'FQxWmiftABjugBx2S3zFtiH6yrZUDW', imgUrl($imgUrl)),
                            'Side' => "face",
                        ],
                    ])
                    ->request();
                $res = $result->toArray();
                return array('status'=>true,'sex'=>$res['Data']['FrontResult']['Gender'],
                    'name'=>$res['Data']['FrontResult']['Name'],
                    'IDNumber'=>$res['Data']['FrontResult']['IDNumber']);
//                return $result->toArray();
            } catch (ClientException $e) {
                return array('status' => false, 'msg' => '请上传身份证正面照片');
//                return $e->getErrorMessage() . PHP_EOL;
            } catch (ServerException $e) {
                return array('status' => false, 'msg' => '请上传身份证正面照片');
//                return $e->getErrorMessage() . PHP_EOL;
            }
        } else {
            return array('status' => false, 'msg' => '请上传身份证照片');
        }
    }

    function OcrBankCard($imgUrl){
        if (!empty($imgUrl)) {
            AlibabaCloud::accessKeyClient('LTAI4G9Q8mU7xavjS1zL1y4q', 'FQxWmiftABjugBx2S3zFtiH6yrZUDW')
                ->regionId('cn-shanghai')
                ->asDefaultClient();
            try {
                $result = AlibabaCloud::rpc()
                    ->product('ocr')
                    // ->scheme('https') // https | http
                    ->version('2019-12-30')
                    ->action('RecognizeBankCard')
                    ->method('POST')
                    ->host('ocr.cn-shanghai.aliyuncs.com')
                    ->options([
                        'query' => [
                            'RegionId' => "cn-shanghai",
                            'ImageURL' => ViapiUtils::upload('LTAI4G9Q8mU7xavjS1zL1y4q', 'FQxWmiftABjugBx2S3zFtiH6yrZUDW', imgUrl($imgUrl)),
                            'Side' => "face",
                        ],
                    ])
                    ->request();
                $res = $result->toArray();
                return array('status'=>true,'CardNumber'=>$res['Data']['CardNumber']);
//                return $result->toArray();
            } catch (ClientException $e) {
                return array('status' => false, 'msg' => '请上传银行卡正面照片');
//                return $e->getErrorMessage() . PHP_EOL;
            } catch (ServerException $e) {
                return array('status' => false, 'msg' => '请上传银行卡正面照片');
//                return $e->getErrorMessage() . PHP_EOL;
            }
        } else {
            return array('status' => false, 'msg' => '请上传银行卡图片');
        }
    }
}